package cn.hyy.FVOnline.service.impl;

import cn.hyy.FVOnline.entity.User;
import cn.hyy.FVOnline.entity.PageEntitys;
import cn.hyy.FVOnline.mapper.UserMapper;
import cn.hyy.FVOnline.service.UserService;
import cn.hyy.FVOnline.utils.Constant;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public JSONObject register(User user) {

        JSONObject jo = new JSONObject();

        User u = userMapper.findByTelephone(user);

        if(u == null){
            userMapper.insertForRegister(user);
            jo.put("code","success");
            jo.put("message","注册成功！");
        }else {
            jo.put("code","error");
            jo.put("message","手机号码已被注册");
        }
        return jo;
    }

    @Override
    public User findByTelephonePassword(User user) {
        return userMapper.findByTelephonePassword(user);
    }

    @Override
    public User findById(User user) {
        return userMapper.findById(user);
    }

    @Override
    public BufferedImage findHS(String userId) {

        InputStream is = null;
        BufferedImage bi = null;

        try {

            User user = new User();
            user.setId(Integer.parseInt(userId));
            User u = userMapper.findById(user);

            if (u.getHsPath() != null){
                is = new FileInputStream(u.getHsPath());
            }else {
                is = new FileInputStream(Constant.hsPath + "default.png");
            }

            bi = ImageIO.read(is);

        }catch (Exception e){
        }finally {

            try {
                if (is != null){
                    is.close();
                }
            }catch (Exception e){
                System.out.println(e);
            }
        }

        return bi;
    }

    @Override
    public void updateInformation(User user) {
        userMapper.updateInformation(user);
    }

    @Override
    public void updatePassword(User user) {
        userMapper.updatePassword(user);
    }

    @Override
    public void updateHS(String userId, MultipartFile file) {
        InputStream is = null;
        FileOutputStream os = null;
        try {
            File hs = new File(Constant.hsPath + userId + ".png");
            if (hs.exists()){
                hs.delete();
            }
            if (!hs.exists()){
                hs.createNewFile();
            }
            is = file.getInputStream();
            os = new FileOutputStream(hs);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) > 0) {
                os.write(buffer, 0, len);
            }

            User user = new User();
            user.setId(Integer.parseInt(userId));
            user.setHsPath(Constant.hsPath + userId + ".png");
            userMapper.updateHS(user);

        }catch (Exception e){
        }finally {
            try {
                if (is != null){
                    is.close();
                }
                if (os!=null){
                    os.close();
                }
            }catch (Exception e){}
        }
    }


    @Override
    public PageEntitys getUser(String what, Integer page, Integer pageSize) {
        if (what == null || what.length() == 0){
            what = "%" + what + "%";
        }
        Integer start = (page - 1) * pageSize;
        List<User> user = userMapper.getUser(what,start,pageSize);
        PageEntitys pageEntitys = new PageEntitys();
        pageEntitys.setUsers(user);
        pageEntitys.setTotal(userMapper.getUserTotal(what));
        return pageEntitys;
    }

    @Override
    public void delUser(Integer id) {
        userMapper.delUser(id);
    }
}
